<template>
	<view class="posi-container">
		<navBar :title='title'></navBar>
		<view class="posi-menu">
			<view :class="menuIndex === index ? 'menu-item menu-item-active' : 'menu-item'"
			 v-for="(item, index) in menuData"
			 :key="index"
			 @click="jumpMenu(index)">
				{{ item.name }}
			</view>
		</view>
		<view class="posi-main">
			<scrollView
				class='scroll'
				:scrollBoolean="listData.total" 
				:scrollTop="listData.scrollTop"
				:scrollLoad="listData.total > listData.list.length" 
				:refresherRriggered="listData.refresherRriggered"
				@refresherrefresh="refresherrefresh" 
				@scrolltolower="scrolltolower">
				<view class="main-list">
					<view class="list-item"
					 v-for="(item, index) in listData.list"
					 :key="index"
					 @click="btn(3, item)"
					>
						<view class="item-title">
							<view class="title-name">{{ item.positionName }}</view>
							<view class="title-money">{{ item.money }}</view>
						</view>
						<view class="item-desc">{{ item.workPositionDesc }}</view>
						<view class="item-border">
							<view class="border-item">{{ setName(item.workType) }}</view>
							<view class="border-item">{{ setName1(item.workAddressType) }}</view>
						</view>
						<view class="" @tap.stop.prevent v-if="item.positionState === 'RELEASE'">
							<switch style="transform:scale(0.7)" :checked="item.positionState === 'RELEASE'" @change="changeItne(item)" />
						</view>
						<view class="item-bt" v-if="item.positionState === 'SUSPEND'">
								<text class="one" @click.stop="btn(1, item)">编辑</text>
								<text class="two" @click.stop="btn(2, item)">发布</text>
						</view>
					</view>
				</view>
				
			</scrollView>
		</view>
		<view :class="isHeight ? 'posi-btns' : 'posi-btn'">
			<button class="btns" @click="openFb">发布职位</button>
		</view>
	</view>
</template>

<script setup>
import navBar from '@/components/navBar.vue'
import { ref, reactive, onMounted } from "vue"
import { onLoad } from '@dcloudio/uni-app'
import commonNavBar from "@/components/commonNavBar.vue";
import scrollView from "@/components/scrollView.vue";
import { getStatusBarHeight, convertToK } from '@/utils/common/common.js'
import api from '@/utils/api/recruit.js'
import { showToast } from '../../utils/toast';
onMounted(_ => {
	getList()
})
onLoad(_ => {
	uni.$on('refsh', _ => {
		listData.list = []
		getList()
	})
})
const isHeight = ref(false)
const info = uni.getSystemInfoSync();
isHeight.value = info.deviceBrand === 'huawei' ? true : false
const barHeight = ref(null)	
const title = ref('职位管理')
// 列表数据
const listData = reactive({
	total: 0, //	总条数
	scrollTop: 0, //	滚动区域
	refresherRriggered: false, //	下拉状态
	pageInfo: { pageNo: 1, pageSize: 10 , positionState: '' },
	list: []
})
const menuData = ref([
	{ name: '全部' },
	{ name: '待发布' },
	{ name: '已发布' },
])
const setName = (positionName) => {
	// 根据 positionName 返回不同的文字
	switch (positionName) {
		case 'FULL_TIME':
			return '全职';
		case 'PART_TIME_JOB':
			return '兼职';
		default:
			return '未知';
	}
}
const setName1 = (positionName) => {
	// 根据 positionName 返回不同的文字
	switch (positionName) {
		case 'OFFLINE':
			return '线下';
		case 'ONLINE':
			return '线上';
		default:
			return '未知';
	}
}
// 暂停职位
const changeItne = async (item) => {
	const res = await api.setPublishPosition({id: item.id})
	if(res.code === 200) {
		showToast(res.msg)
		listData.list = []
		listData.pageInfo.pageNo = 1
		listData.pageInfo.pageSize = 10
		getList()
	}
}
// 刷新
const refresherrefresh = _ => {
	listData.refresherRriggered = true;
	listData.list = [];
	listData.pageInfo = { pageNo: 1, pageSize: 10 };
	getList();
}
// 触底事件
const scrolltolower = _ => {
	if (listData.list.length >= listData.total) return false;
		listData.pageInfo.pageNo++;
		getList();	
}
// 菜单索引
const menuIndex = ref(0)
// 点击菜单
const jumpMenu = index => {
	menuIndex.value = index
	listData.list = []
	listData.pageInfo = { pageNo: 1, pageSize: 10 };
	getList()
	
}
// 编辑发布
const btn = (type, val) => {
	console.log(val);
	let datas = {
		type: type,
		...val
	}
	uni.navigateTo({ url:`/pages/positionManagement/detail?data=${JSON.stringify(datas) }` })
}
// 获取列表
const getList = async _ => {
	listData.pageInfo.positionState = menuIndex.value === 0 ? '' :  menuIndex.value === 1 ? 'SUSPEND' : 'RELEASE'
	const res = await api.queryPublishPosition(listData.pageInfo)
	listData.list = listData.list.concat(res.data.list);
	listData.list = listData.list.map(item=> {
		return {
			money: `${ convertToK(item.salaryMin) } - ${convertToK(item.salaryMax)}`,
			...item
		}
	})
	listData.total = res.data.total;
	listData.refresherRriggered = false;
}
// 前往发布职位
const openFb = _ => {
	uni.navigateTo({
		url: '/pages/postPosition/postPosition'
	})
}

</script>

<style lang="scss">
#app{
	background: #F4F6FA;
}	
.posi-container{
	height: 100vh;
	overflow: hidden;
	background: #F4F6FA;
	.posi-menu{
		width: 750rpx;
		height: 98rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 32rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		.menu-item{
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: normal;
			font-size: 28rpx;
			color: #595959;
			line-height: 42rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
			margin-right: 19rpx;
		}
		.menu-item-active{
			color: #6B64D3;
			font-weight: bold;
		}
	}
	.posi-main{
		height: calc(100% - 300rpx);
		
		.scroll{
			height: 100%;
			.main-list{
				height: 100%;
				padding: 24rpx;
				box-sizing: border-box;
				.list-item{
					background: #FFFFFF;
					border-radius: 24rpx 24rpx 24rpx 24rpx;
					padding: 24rpx;
					box-sizing: border-box;
				  margin-bottom: 24rpx;
					.item-title{
						display: flex;
						align-items: center;
						justify-content: space-between;
						.title-name{
							font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
							font-weight: normal;
							font-size: 36rpx;
							color: #262626;
							line-height: 54rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
						}
						.title-money{
							font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
							font-weight: normal;
							font-size: 32rpx;
							color: #6B64D3;
							line-height: 48rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
						}
					}
					.item-desc{
						font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
						font-weight: normal;
						font-size: 28rpx;
						color: #262626;
						line-height: 42rpx;
						text-align: left;
						font-style: normal;
						text-transform: none;
						margin-top: 16rpx;
					}
					.item-border{
						margin-top: 16rpx;
						display: flex;
						align-items: center;
						padding-bottom: 24rpx;
						.border-item{
							padding: 6rpx 16rpx;
							box-sizing: border-box;
							background: #F4F6FA;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
							font-weight: normal;
							font-size: 24rpx;
							color: #8C8C8C;
							line-height: 36rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
							margin-right: 12rpx;
						}
					}
					.item-bt{
						padding-top: 24rpx;
						// padding-bottom: 24rpx;
						border-top: 2rpx solid #F4F6FA;
						box-sizing: border-box;
						display: flex;
						justify-content: flex-end;
						.one{
							border-radius: 32rpx 32rpx 32rpx 32rpx;
							border: 2rpx solid #D9D9D9;
							padding: 11rpx 24rpx;
							box-sizing: border-box;
							font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
							font-weight: normal;
							font-size: 28rpx;
							color: #262626;
							line-height: 42rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
						}
						.two{
							background: #6B64D3;
							border-radius: 32rpx 32rpx 32rpx 32rpx;
							padding: 11rpx 24rpx;
							box-sizing: border-box;
							font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
							font-weight: normal;
							font-size: 28rpx;
							color: #FFFFFF;
							line-height: 42rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
							margin-left: 16rpx;
						}
					}
				}
			}
		}
	}
	.posi-btn{
		left: 0;
		width: 100%;
		bottom: 0;
		position: fixed;
		padding-bottom: 40rpx;
		padding-top: 10rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		.btns{
			width: 640rpx;
			height: 88rpx;
			background: #6B64D3;
			border-radius: 44rpx 44rpx 44rpx 44rpx;
			color: #fff;
		}
	}
	.posi-btns{
		// padding-bottom: 100rpx;
		position: fixed;
		// height: 200rpx;
		padding-top: 10rpx;
		bottom: 0rpx;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		.btns{
			width: 640rpx;
			height: 88rpx;
			background: #6B64D3;
			border-radius: 44rpx 44rpx 44rpx 44rpx;
			color: #fff;
		}
	}
}
</style>
